import axios from 'axios'

const api = axios.create({
    baseURL:'/api',
    timeout:10000
})

api.interceptors.request.use(
    config => {
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

api.interceptors.response.use(
    response => {
        return response.data
    },
    error => {
        console.error('API请求错误：', error)
        if (error.response) {
            // 服务器返回错误状态码
            console.error('错误状态码：', error.response.status)
            console.error('错误信息：', error.response.data)
        } else if (error.request) {
            // 请求已发出但没有收到响应
            console.error('网络错误：无法连接到服务器')
        } else {
            // 其他错误
            console.error('请求配置错误：', error.message)
        }
        return Promise.reject(error)
    }
)

export const userAPI = {
    login: (user_id, password) => api.get(`/User/${user_id}`),
    add: (data) => api.post('/User', data),
    select: (user_id) => api.get(`/User/${user_id}`),
    update: (data) => api.put('/User', data),
    delete: (user_id) => api.delete(`/User/${user_id}`)
}

export const inBoundAPI = {
    add: (data) => api.post('/InBound', data),
    select: (orderId) => api.get(`/InBound/${encodeURIComponent(orderId)}`),
    update: (data) => api.put('/InBound', data),
    delete: (orderId) => api.delete(`/InBound/${encodeURIComponent(orderId)}`)
}

export const outBoundAPI = {
    add: (data) => api.post('/OutBound', data),
    select: (billId) => api.get(`/OutBound/${encodeURIComponent(billId)}`),
    update: (data) => api.put('/OutBound', data),
    delete: (billId) => api.delete(`/OutBound/${encodeURIComponent(billId)}`)
}

export const businessAPI = {
    add: (data) => api.post('/Business', data),
    select: (bizId) => api.get(`/Business/${encodeURIComponent(bizId)}`),
    update: (data) => api.put('/Business', data),
    delete: (bizId) => api.delete(`/Business/${encodeURIComponent(bizId)}`)
}